﻿using AutoMapper;
using System.Collections.ObjectModel;
using ToDo.API.Context;
using ToDo.API.Context.UnitOfWork;
using ToDo.Shared;
using ToDo.Shared.Models;

namespace ToDo.API.Service
{
    /// <summary>
    /// 待办事项的实现
    /// </summary>
    public class ToDoService : IToDoService
    {
        private readonly IUnitOfWork work;
        private readonly IMapper Mapper;

        public ToDoService(IUnitOfWork work, IMapper mapper)
        {
            this.work = work;
            Mapper = mapper;
        }

        //public async Task<ApiResponse> AddAsync(DailyDto model)
        //{
        //    try
        //    {
        //        var todo = Mapper.Map<Daily>(model);
        //        await work.GetRepository<Daily>().InsertAsync(todo);
        //        if (await work.SaveChangesAsync() > 0)
        //            return new ApiResponse(true, todo);
        //        return new ApiResponse("添加数据失败");
        //    }
        //    catch (Exception ex)
        //    {
        //        return new ApiResponse(ex.Message);
        //    }
        //}
        public async Task<ApiResponse> AddAsync(DailyDto model)
        {
            try
            {
                var todo = Mapper.Map<Daily>(model);

                await work.GetRepository<Daily>().InsertAsync(todo);
                if (await work.SaveChangesAsync() > 0)
                    return new ApiResponse(true, todo);
                return new ApiResponse("添加数据失败");
            }
            catch (Exception ex)
            {
                return new ApiResponse(ex.Message);
            }
        }

        public async Task<ApiResponse> DeleteAsync(int id)
        {
            try
            {
                var repository = work.GetRepository<Daily>();
                var todo = await repository
                    .GetFirstOrDefaultAsync(predicate: x => x.Id.Equals(id));
                repository.Delete(todo);
                if (await work.SaveChangesAsync() > 0)
                    return new ApiResponse(true, "");
                return new ApiResponse("删除数据失败");
            }
            catch (Exception ex)
            {
                return new ApiResponse(ex.Message);
            }
        }

        public async Task<ApiResponse> GetAllAsync(Page parameter)
        {
            try
            {
                var repository = work.GetRepository<Daily>();
                var todos = await repository.GetPagedListAsync(
                   pageIndex: parameter.PageIndex,
                   pageSize: parameter.PageSize);
                return new ApiResponse(true, todos);
            }
            catch (Exception ex)
            {
                return new ApiResponse(ex.Message);
            }
        }

        public async Task<ApiResponse> GetAllAsync(ToDoParameter parameter)
        {
            try
            {
                var repository = work.GetRepository<Daily>();
                var todos = await repository.GetPagedListAsync(
                   pageIndex: parameter.PageIndex,
                   pageSize: parameter.PageSize);
                return new ApiResponse(true, todos);
            }
            catch (Exception ex)
            {
                return new ApiResponse(ex.Message);
            }
        }

        public async Task<ApiResponse> GetSingleAsync(int id)
        {
            try
            {
                var repository = work.GetRepository<Daily>();
                var todo = await repository.GetFirstOrDefaultAsync(predicate: x => x.Id.Equals(id));
                return new ApiResponse(true, todo);
            }
            catch (Exception ex)
            {
                return new ApiResponse(ex.Message);
            }
        }
      
        public async Task<ApiResponse> UpdateAsync(DailyDto model)
        {
            try
            {
                var dbToDo = Mapper.Map<Daily>(model);
                var repository = work.GetRepository<Daily>();
                var todo = await repository.GetFirstOrDefaultAsync(predicate: x => x.Id.Equals(dbToDo.Id));

                todo.Title = dbToDo.Title;

                repository.Update(todo);

                if (await work.SaveChangesAsync() > 0)
                    return new ApiResponse(true, todo);
                return new ApiResponse("更新数据异常！");
            }
            catch (Exception ex)
            {
                return new ApiResponse(ex.Message);
            }
        }
    }
}
